import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('1234567890abcdef', 10)
export const jsonp = function (url, data) {
  return new Promise((resolve, reject) => {
    // 1.初始化url
    const dataString = url.indexOf('?') === -1 ? '?' : '&'
    const callbackName = `jsonpCB_${nanoid()}`
    url += `${dataString}callback=${callbackName}`
    if (data) {
      // 2.有请求参数，依次添加到url
      for (const k in data) {
        url += `&${k}=${data[k]}`
      }
    }

    const scriptNode = document.createElement('script')
    scriptNode.src = url

    // 3. callback
    window[callbackName] = (result) => {
      result ? resolve(result) : reject('没有返回数据')
      delete window[callbackName]
      document.body.removeChild(scriptNode)
    }

    // 4. 异常情况
    scriptNode.addEventListener(
      'error',
      () => {
        reject('接口返回数据失败')
        delete window[callbackName]
        document.body.removeChild(scriptNode)
      },
      false,
    )

    // 5. 开始请求
    document.body.appendChild(scriptNode)
  })
}
